//
//  RJStatusTableCell.m
//  AirQuality
//
//  Created by Po on 15/5/7.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJStatusTableCell.h"

@interface RJStatusTableCell()

@property (assign, nonatomic) BOOL haveData;

@end

@implementation RJStatusTableCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"RJStatusTableCell" owner:self options:nil];
        UIView * view = array[0];
        [view setFrame:frame];
        [self addSubview:view];
        _haveData = NO;
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setCityName:(NSString *)name
{
    NSMutableAttributedString * colorText = [[NSMutableAttributedString alloc] initWithString:name];
    NSRange range = [colorText.string rangeOfString:@"["];
    [colorText addAttribute:NSForegroundColorAttributeName value:GetRGBColor(85, 85, 85, 1) range:NSMakeRange(0, range.location)];
    NSUInteger start = range.location;
    NSUInteger end = colorText.string.length - start;
    NSString * status = [name substringWithRange:NSMakeRange(start, end)];
    NSString * cityString = [name substringWithRange:NSMakeRange(0, start)];
    NSString * allString = [[cityString stringByAppendingString:@"   "] stringByAppendingString:status];
    if ([status isEqualToString:@"[已完成]"]) {
        _haveData = YES;
    }
    
    UIColor * color = [self getColorWithStatus:status];
    [colorText addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(start, end)];
    
    [self.nameLabel setAttributedText:colorText];
}

- (UIColor *)getColorWithStatus:(NSString *)status
{
    if ([status isEqualToString:@"[已完成]"]) {
        return GetRGBColor(0, 255, 1, 1);
    } else if ([status isEqualToString:@"[已下发]"]){
        return GetRGBColor(0, 255, 1, 0.5);
    } else {
        return [UIColor redColor];
    }
}

- (void)setAQiNum:(NSString *)num
{
//    if (!_haveData) {
//        [_aqiLabel setText:@"0"];
//        [_aqiRankLabel setText:@"数据尚未审核"];
//        [_aqiRankLabel setBackgroundColor:[UIColor grayColor]];
//        return;
//    }
    
    [_aqiLabel setText:num];
    NSArray * rankArray = @[@"优",@"良",@"轻度",@"中度",@"重度",@"严重",@"无"];
    NSInteger rank = [num integerValue] / 50;
    if ([num integerValue] % 50 == 0) {
        rank -- ;
    }
    
    if (rank > 5) {
        rank = 5;
    }
    
    if (rank < 0) {
        rank = 6;
    }
    
    NSString * rankString = rankArray[rank];
    
    [_aqiRankLabel setText:rankString];
    [_aqiRankLabel setTextColor:[UIColor whiteColor]];
    UIColor * color = [RJTools getColorWithAirNum:[num floatValue] type:@"AQI" alpha:1];
    [_aqiLabel setTextColor:color];
    _aqiRankLabel.layer.cornerRadius = 5;
    [_aqiRankLabel setBackgroundColor:color];
    
}

- (void)setPM25Num:(NSString *)num
{
    if (!_haveData) {
        [_PM25Label setText:@"0"];
        return;
    }
    [_PM25Label setText:num];
}

- (void)setPM10Num:(NSString *)num
{
    if (!_haveData) {
        [_PM10Label setText:@"0"];
        return;
    }
    [_PM10Label setText:num];
}

- (void)setO3Num:(NSString *)num
{
    if (!_haveData) {
        [_O3Label setText:@"0"];
        return;
    }
    [_O3Label setText:num];
}

- (void)setPollutionString:(NSString *)string
{
    if ([string isEqualToString:@"—"]) {
        [_pollutionLabel setText:@"无"];
        return;
    }
    [_pollutionLabel setText:string];
}
- (void)setPollutionAttrString:(NSMutableAttributedString *)string{
    
    _pollutionLabel.attributedText = string;
}
@end
